package com.zxw.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxw.common.web.annotation.InitInsertInfo;
import com.zxw.common.web.annotation.InitSelectInfo;
import com.zxw.common.web.annotation.InitUpdateInfo;
import com.zxw.common.web.util.JwtUtils;
import com.zxw.user.domain.dto.RoleDTO;
import com.zxw.user.domain.entity.Button;
import com.zxw.user.domain.entity.Role;
import com.zxw.user.domain.vo.RolePageVO;
import com.zxw.user.mapper.RoleMapper;
import com.zxw.user.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Xuewei.Zhang
 * @since 2022-01-21
 */
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    private final RoleMapper roleMapper;

    @Override
    public Role findRoleById(Long id) {
        Long companyId = JwtUtils.getCompanyId();
        return roleMapper.findByIdAndCompanyId(id, companyId);
    }

    @Override
    @InitSelectInfo
    public List<RoleDTO> roleList(RolePageVO rolePageVO) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", rolePageVO.getCompanyId());
        List<RoleDTO> roleList = roleMapper.selectList(wrapper);
        return roleList;
    }

    @Override
    @InitSelectInfo
    public IPage<RoleDTO> roleListPage(RolePageVO rolePageVO) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", rolePageVO.getCompanyId());
        IPage<RoleDTO> iPage = new Page<>(rolePageVO.getPageNum(), rolePageVO.getPageSize());
        List<RoleDTO> roleList = roleMapper.getPageRoleList(iPage, rolePageVO);
        iPage.setRecords(roleList);
        return iPage;
    }

    @Override
    @InitInsertInfo
    public Role addRole(Role role) {
        roleMapper.insert(role);
        return role;
    }

    @Override
    @InitUpdateInfo
    public Role update(Role role) {
        roleMapper.updateById(role);
        return role;
    }

    @Override
    public void delete(String ids) {
        Long companyId = JwtUtils.getCompanyId();
        QueryWrapper<Role> queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        queryWrapper.eq("company_id", companyId);
        roleMapper.delete(queryWrapper);
    }
}
